package com.zizy.healthexpert.Activity;

import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;

import com.zizy.healthexpert.Config.Config;
import com.zizy.healthexpert.Database.MyDatabase_PersonalInf;
import com.zizy.healthexpert.R;
import com.zizy.healthexpert.Adapter.listAddDiseaseAdapter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class SettingDiseaseActivity extends ActionBarActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting_disease);

        ListView listView=(ListView) findViewById(R.id.listAddDisease);
        List list= new ArrayList< listAddDiseaseAdapter.AddedDisease>(); //TODO

        /* set list*/
        list.add(new listAddDiseaseAdapter.AddedDisease(1,"糖尿病",null,null,null));// TODO but not here
        list=Config.Disease.list;
        /*set view*/
        listAddDiseaseAdapter adapter=new listAddDiseaseAdapter(getApplicationContext(),R.layout.list_disease,list);
        listView.setAdapter(adapter);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
       getMenuInflater().inflate(R.menu.setting_disease, menu);
        return true;
    }
    public String getDiseaseList(){
        String res="";
        ArrayList<String> list=listAddDiseaseAdapter.checkedList;
        for(Iterator iterator=list.iterator();iterator.hasNext();){
            String str=(String)iterator.next();
            res+=str+",";

        }
        return res;
    }
    //clicked the button ok TODO
    public void btnAddedDiseaseOKClicked(View v){
        /*open database*/


        MyDatabase_PersonalInf myDatabase_personalInf=new MyDatabase_PersonalInf(getApplicationContext());
             /*first   */
        if (myDatabase_personalInf.SearchDatabase("*","ID="+ Config.userID).size()==0){
            myDatabase_personalInf.insertDatabase(Config.userID,Config.userName,Config.userAppellation,getDiseaseList());
        }else{
            myDatabase_personalInf.updateDatabase(Config.userID, Config.userName,Config.userAppellation,getDiseaseList() );
        }
         /*over*/
        myDatabase_personalInf.close();
        Toast.makeText(getApplicationContext(),"個人信息記錄成功",Toast.LENGTH_LONG).show();
        /*intent*/
        MainActivity.instance.finish();
        startActivity(new Intent(SettingDiseaseActivity.this,MainActivity.class));
        this.finish();

    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
